#include "Weapon.h"
#include "Util.h"
#include "Game.h"
#include "StringLib.h"

Weapon::Weapon(GenericType genericType, bool highQuality) : EquippableItem(genericType) {
	m_highQuality = highQuality;
}

Weapon::Weapon(GenericType genericType, std::ifstream& file) : EquippableItem(genericType, file) {
	int* tokens = Util::intSplit(file);
	m_highQuality = tokens[0] == 1;
	m_numDice = tokens[1];
	m_diceSides = tokens[2];
	m_attackBonus = tokens[3];
	m_accuracy = tokens[4];
	m_speed = tokens[5];
	delete[] tokens;
}

Weapon::~Weapon() {
}

void Weapon::save(std::ofstream& file) const {
	EquippableItem::save(file);
	file << (m_highQuality ? 1 : 0) << " " << m_numDice << " " << m_diceSides << " " << m_attackBonus << " " << m_accuracy << " " << m_speed << "\n";
}

int Weapon::getRandomDiceDamage() const {
	int damage = 0;
	for(int i = 0; i < m_numDice; i++) {
		damage += rand_plus_one(m_diceSides);
	}
	damage += m_attackBonus;
	return std::max(0, damage);
}

int Weapon::getRandomDamage() const {
	return getRandomDiceDamage();
}

void Weapon::logDetails(TextLog& log) const {
	Item::logDetails(log);
	log.addLine(g_stringLib->getMiscString(MST_GRIP, m_gripType));
	log.addBlankLine();
	log.addLine(g_stringLib->getTextString(ST_WEAPON_NUM_DICE, (m_equippableIdentified ? Util::intToString(m_numDice).c_str() : Util::intToString(m_defaultNumDice)).c_str()));
	log.addLine(g_stringLib->getTextString(ST_WEAPON_DIE_SIDES, (m_equippableIdentified ? Util::intToString(m_diceSides).c_str() : Util::intToString(m_defaultDiceSides)).c_str()));
	log.addLine(g_stringLib->getTextString(ST_WEAPON_ATTACK_BONUS, (m_equippableIdentified ? Util::signIntToString(m_attackBonus).c_str() : Util::signIntToString(m_defaultAttackBonus)).c_str()));
	log.addLine(g_stringLib->getTextString(ST_WEAPON_ACCURACY_BONUS, (m_equippableIdentified ? Util::signIntToString(m_accuracy).c_str() : Util::signIntToString(m_defaultAccuracy)).c_str()));
	log.addLine(g_stringLib->getTextString(ST_WEAPON_SPEED, (m_equippableIdentified ? Util::intToString(m_speed).c_str() : Util::intToString(m_defaultSpeed)).c_str()));
	log.addBlankLine();
	if(m_equippableIdentified) {
		if(m_highQuality)
			log.addLine(g_stringLib->getTextString(ST_WEAPON_HIGH_QUALITY));
	}
}
